import config from '@/config.js'
console.log(config, 'fonfig')
let total = function(arr) {
    var s = 0
    arr.forEach(function(val, idx, arr) {
        s += val
    }, 0)
    return s
}
/**
 * 时间格式化
 * @param {object,string} date = 时间
 * @param {string} format = 'yyyy-MM-dd'
 */
const dateFilter = (date, format = 'yyyy-MM-dd') => {
    // format: string -> yyyy-MM-dd hh:mm:ss
    if (!date) return '暂无时间'
    if (date.length < 20) {
        date = date.replace(/-/g, '/').replace(/\./g, '/')
    }
    let d = new Date(date)
    let year = d.getFullYear()
    let month = (d.getMonth() + 1).toString().padStart(2, 0)
    let day = d
        .getDate()
        .toString()
        .padStart(2, 0)
    let hour = d
        .getHours()
        .toString()
        .padStart(2, 0)
    let minute = d
        .getMinutes()
        .toString()
        .padStart(2, 0)
    let second = d
        .getSeconds()
        .toString()
        .padStart(2, 0)

    let weekend = ['天', '一', '二', '三', '四', '五', '六']
    let week = d.getDay()
    return format
        .replace('yyyy', year)
        .replace('MM', month)
        .replace('dd', day)
        .replace('hh', hour)
        .replace('mm', minute)
        .replace('ss', second)
        .replace('week', '星期' + weekend[week])
}
/**
 * 路径过滤补全
 * @param {string} url = '字符串'
 */
const imgFilter = url => {
    if (!url) return ''
    if (url.includes('http') || url.includes('static')) return url
    let baseUrl = config.baseUrl
    return baseUrl + url
}
/**
 * 手机号脱敏处理，保留前三后四
 * @param {string} value = 传入手机号
 */
const replacestar = value => {
    if (!value) return ''
    let str = value
    str = str.replace(new RegExp('[^0-9]+', 'g'), '')
    if (str.length == 11) {
        str = str.toString().replace(/(\d{3})\d*(\d{4})/, '$1****$2')
    } else {
        str = str.toString().replace(/(\d{3})\d*(\d{4})/, '$1******$2')
    }
    return str
}
/**
 * 数字转大写
 * @param {string,number} temp = '字符串'
 */
const numFormatter = temp => {
    if (temp === 0) return '零'
    let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
    let unit = ['', '十', '百', '千', '万']
    let strArr = temp
        .toString()
        .split('')
        .reverse()
    let newNum = ''
    if (temp < 100) {
        for (var i = 0; i < strArr.length; i++) {
            newNum =
                // temp为0时,newNum为空
                (i == 0 && strArr[i] == 0 && strArr.length < 3
                    ? ''
                    : // temp为0时,newNum为空
                    i > 0 && strArr[i] == 0 && strArr[i - 1] == 0
                    ? ''
                    : // 10~19以内 不是10
                    strArr[i] == 1 && strArr[i - 1] <= 9 && strArr[i - 1] != 0
                    ? unit[i] + changeNum[strArr[i - 1]]
                    : // 10~19以内 是10
                    strArr[i] == 1 && strArr[i - 1] <= 9 && strArr[i - 1] == 0 && strArr.length < 3
                    ? unit[i]
                    : // 10~19以外的数字
                      changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i])) + (strArr[i] == 1 && strArr[i - 1] <= 9 ? '' : newNum)
        }
        return newNum
    } else {
        for (var i = 0; i < strArr.length; i++) {
            newNum = (i == 0 && strArr[i] == 0 ? '' : i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? '' : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i])) + newNum
        }
        return newNum
    }
}
const timestamp = (time, hour = 0) => {
    let timestamp = new Date(dateFilter(time, 'yyyy/MM/dd hh:mm')).getTime()
    let curr = new Date().valueOf()
    let aDay = 3600 * 1000 * hour
    return (timestamp + aDay - curr) / 1000
}
/**
 * 比上期
 * @param {number} newNum = '本期数'
 * @param {number} oldNum = '上期数'
 * @param {string} str = '符号'
 */
const coeff = (newNum, oldNum, str = '') => {
    let a = Number(Math.abs(newNum - oldNum))
    if (isNaN(a)) return ''
    let b = Boolean(a > 10000) ? Number(a / 10000).toFixed(2) + '万' : a.toFixed(2)
    return b + str
}
/**
 * 正负数
 * @param {number} newNum = '本期数'
 * @param {number} oldNum = '上期数'
 */
const sign = (newNum, oldNum) => {
    // if(newNum-oldNum===0) return true
    return newNum - oldNum > 0
}
/**
 * 匹配超额省略加上单位
 * @param {number} Num = '源数值'
 */
const matchingNnit = Num => {
    let a = Number(Num)
    let b = a > 10000 ? Number(a / 10000).toFixed(2) + '万' : a.toFixed(2)
    return b
}

export { total, dateFilter, imgFilter, replacestar, numFormatter, timestamp, coeff, sign, matchingNnit }
